package com.duoketech.controller.deliveryconfig;

import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.deliveryconfig.DeliveryConfig;
import com.duoketech.service.deliveryconfig.DeliveryConfigService;
import com.duoketech.utils.PageUtil;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class DeliveryConfigController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",10);
        Page<DeliveryConfig> result = (Page<DeliveryConfig>) DeliveryConfigService.ME.pageAllRecord(getModel(DeliveryConfig.class),pageNum,pageSize,Kv.create());
        for (DeliveryConfig e:result.getList()){
            e.set(DeliveryConfig.FIRST_WEIGHT_PRICE,e.firstWeightPrice());
            e.set(DeliveryConfig.FIRST_WEIGHT,e.firstWeight());
            e.set(DeliveryConfig.ADDITIONAL_WEIGHT_PRICE,e.additionalWeightPrice());
            e.set(DeliveryConfig.ADDITIONAL_WEIGHT,e.additionalWeight());
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            DeliveryConfig deliveryConfig = (DeliveryConfig) DeliveryConfigService.ME.queryBy(id,getModel(DeliveryConfig.class),Kv.create());
            deliveryConfig.set(DeliveryConfig.NAME,deliveryConfig.getStr(DeliveryConfig.NAME)+","+deliveryConfig.getStr(DeliveryConfig.DELIVERY_CODE));
            setAttr("deliveryConfig",deliveryConfig);
            setAttr("province",DeliveryConfigService.ME.queryRegionOfProvince());
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void add(){
        setAttr("province",DeliveryConfigService.ME.queryRegionOfProvince());
        render("add-"+viewModel()+".html");
    }

    @Override
    public void doAdd() {
        try {
            DeliveryConfig deliveryConfig = getModel(DeliveryConfig.class);
            String firstWeight = getPara("first_weight","1.0");
            String firstWeightPrice = getPara("first_weight_price","1.0");
            String additinalWeight = getPara("additional_weight","1.0");
            String additinalWeightPrice = getPara("additional_weight_price","1.0");
            String[] area = getParaValues("area");

            deliveryConfig.set(DeliveryConfig.ADDITIONAL_WEIGHT,PageUtil.kg2G(Double.valueOf(additinalWeight)));
            deliveryConfig.set(DeliveryConfig.ADDITIONAL_WEIGHT_PRICE,PageUtil.yuanToFeng(Double.valueOf(additinalWeightPrice)));
            deliveryConfig.set(DeliveryConfig.FIRST_WEIGHT,PageUtil.kg2G(Double.valueOf(firstWeight)));
            deliveryConfig.set(DeliveryConfig.FIRST_WEIGHT_PRICE,PageUtil.yuanToFeng(Double.valueOf(firstWeightPrice)));
            deliveryConfig.set(DeliveryConfig.AREA,PageUtil.split(area));

            DeliveryConfigService.ME.doAdd(deliveryConfig,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            DeliveryConfig deliveryConfig = getModel(DeliveryConfig.class);
            String firstWeight = getPara("first_weight","1.0");
            String firstWeightPrice = getPara("first_weight_price","1.0");
            String additinalWeight = getPara("additional_weight","1.0");
            String additinalWeightPrice = getPara("additional_weight_price","1.0");
            String[] area = getParaValues("area");

            deliveryConfig.set(DeliveryConfig.ADDITIONAL_WEIGHT,PageUtil.kg2G(Double.valueOf(additinalWeight)));
            deliveryConfig.set(DeliveryConfig.ADDITIONAL_WEIGHT_PRICE,PageUtil.yuanToFeng(Double.valueOf(additinalWeightPrice)));
            deliveryConfig.set(DeliveryConfig.FIRST_WEIGHT,PageUtil.kg2G(Double.valueOf(firstWeight)));
            deliveryConfig.set(DeliveryConfig.FIRST_WEIGHT_PRICE,PageUtil.yuanToFeng(Double.valueOf(firstWeightPrice)));
            deliveryConfig.set(DeliveryConfig.AREA,PageUtil.split(area));

            DeliveryConfigService.ME.update(deliveryConfig,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        DeliveryConfigService.ME.deleteBy(id,getModel(DeliveryConfig.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }
  }